let app = new Vue({
    el: '#app',
    data: {
        user: JSON.parse(sessionStorage.getItem('user')),
        unreadNum: sessionStorage.getItem("unreadNum"),
        examPaperOne: [],
        examPaperTwo: [],
        examPaperThree: [],
        userTasks:[]
    },
    created() {
        // 查询当前用户的未读消息数
        $.ajax({
            url: 'http://localhost:8080/message/selectAll',
            data: { receiveUserId: this.user.userId, status: 1 },
            type: 'get',
            success: function (vo) {
                if (vo.code == 200) {
                    sessionStorage.setItem("unreadNum", vo.data.length);
                }
            }
        });

        // 查询全部试卷
        $.ajax({
            url: 'http://localhost:8080/examPaper/select',
            data: { levelName: this.user.userLevel},
            type: 'get',
            success: function (vo) {
                if (vo.code == 200) {
                    // 遍历循环，根据试卷类型进行分类
                    $.each(vo.data,function(index,e){
                        if(e.paperType==1){
                            app.examPaperOne.push(e);
                        }else if(e.paperType==2){
                            app.examPaperTwo.push(e);
                        }else if(e.paperType==3){
                            app.examPaperThree.push(e);
                        }
                        
                    });
                    
                }
            }
        });

        // 查询当前用户的用户任务
        $.ajax({
            url: 'http://localhost:8080/task/selectByLevelName',
            data: { userId: this.user.userId, levelName:this.user.userLevel},
            type: 'get',
            success: function (vo) {
                if (vo.code == 200) {
                    // reverse() : 翻转数组，倒序
                    app.userTasks=vo.data.reverse();
                    console.log(app.userTasks);
                }
            }
        });


    },
    // vue.js 生命周期：updated 就是 数据加载完之后才执行
    updated() {
        // 渲染layui风琴面板
        layui.element.render();
    },
    methods: {
        demo: function (exam,userTaskId) {
            //询问框
            layer.confirm('您即将进行《' + exam.examPaperName + '》答题，是否继续?', {
                icon: 3,
                closeBtn: 0,  // 右上角关闭
                resize: false,  // 是否允许拉伸
                move: false,    // 是否允许拖拽
                btn: ['开始答题', '取消'] //按钮
            }, function (index) {
                layer.close(index);
                // sessionStorage.setItem("userTaskId",userTaskId);
                if(userTaskId==null || userTaskId==undefined){
                    sessionStorage.setItem("exam", JSON.stringify(exam));
                    sessionStorage.setItem("changeCount", 0);    // 初始化切屏记录数
                    $.cookie("djsendtime", null);               //初始化cookie倒计时
                    LockButton('#Time', exam.suggestTime);
                    window.open("paper_test.html");
                }else{
                    $.ajax({
                        url:'http://localhost:8080/userTask/update',
                        data:{'userTaskId':userTaskId,status:2},
                        type:'post',
                        success:function(vo){
                            if(vo.code==200){
                                sessionStorage.setItem("exam", JSON.stringify(exam));
                                sessionStorage.setItem("changeCount", 0);    // 初始化切屏记录数
                                $.cookie("djsendtime", null);               //初始化cookie倒计时
                                LockButton('#Time', exam.suggestTime);
                                window.open("paper_test.html");
                            }
                        }
                    });
                }
                
                
            });

        },
        lookPaper:function(index){
            window.open("exam_record.html");
        }
    },
    filters:{
        dateFormat:function(d){
            let arr = d.split(" ");
            return arr[0];
        }
    }
});